package com.kanshu.ksgb.fastread.doudou.common.net.rx;

import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;

import io.reactivex.functions.Consumer;

/**
 * rxjava 观察者 封装
 * Created by wcy on 2017/4/22.
 */
public abstract class BaseConsumer<T> implements Consumer<BaseResult<T>> {

    public final int SUCCESS_CODE = 0;

    public final int FAIL_CODE = -1;

    @Override
    public void accept(BaseResult<T> tBaseResult) throws Exception {
        if (tBaseResult.result != null && tBaseResult.result.status != null) {
            if (tBaseResult.result.status.code == SUCCESS_CODE) {
                onResponse(tBaseResult, tBaseResult.result.data);
            } else {
                onError(tBaseResult.result.status.code, tBaseResult.result.status.msg);
            }
        } else {
            onError(FAIL_CODE, "no data");
        }
    }

    public abstract void onResponse(BaseResult<T> tBaseResult, T t);

    public void onError(int code, String errDesc) {
    }
}
